package org.adl.registry.website;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.adl.registry.types.Registry;
import org.adl.registry.website.rim.RimProxy;

/**
 * Provides methods for relaying to RIM a non-search request submitted via the ADL Registry website.
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class GenericRequest extends RequestHandler
{
    /**
     * Instantiates a new instance of the <CODE>NonSearchHandler</CODE> class.
     * @param request The <CODE>ServletRequest</CODE> generated by the Servlet receiving the submission of a Form based ADL Registry request.
     */
    public GenericRequest(HttpServletRequest iRequest, ServletContext iContext)
    throws InvalidRequestException
    {
        super.setServletRequest(iRequest);
        super.setServletContext(iContext);
    }
    
    /**
     * Initiates and carries out a communication session with RIM Lite.
     * @return Returns the <CODE>String</CODE> response from a request to RIM Lite. Generally these responses are always XML.
     * @throws java.io.IOException Throws <CODE>IOException</CODE> if communication with RIM Lite fails.
     */
    public String InvokeRequest()
    throws IOException, InvalidRequestException
    {
        String rimResponse = "";
        
        ServletRequest request = super.getServletRequest();
        
        String contentType = request.getContentType();
        
        if (contentType == null)
        {
            rimResponse = this.ProcessUrlEncoded();
        }
        else
        {
            if (contentType.contains("application/x-www-form-urlencoded"))
            {
                rimResponse = this.ProcessUrlEncoded();
            }
            else if (contentType.contains("multipart/form-data"))
            {
                rimResponse = this.ProcessMultipart();
            }
        }
        
        return rimResponse;
    }
    
    private String ProcessMultipart()
    throws IOException
    {
        String rimResponse;
        
        HttpServletRequest request = super.getServletRequest();
        ServletContext context = super.getServletContext();
        
        //TODO: Parse the multipart request to determine what registry to send request to.
        String rimUrl = PortalUtil.getRimUrl(context, Registry.OPERATIONAL_REGISTRY_HANDLE);
        
        RimProxy proxy = new RimProxy(rimUrl);
        
        proxy.openConnection();
        
        proxy.forwardMultipart(request.getInputStream());
        
        rimResponse = proxy.writeResponseTo();
        
        proxy.close();
        return rimResponse;
    }
    
    private String ProcessUrlEncoded()
    throws IOException, InvalidRequestException
    {
        String rimResponse;
        
        HttpServletRequest request = super.getServletRequest();
        ServletContext context = super.getServletContext();
        
        String registry = request.getParameter("registry");
        super.setRegistry(registry);
        
        String rimUrl = PortalUtil.getRimUrl(context, registry);
        
        RimProxy proxy = new RimProxy(rimUrl);
        
        proxy.openConnection();
        
        Map params = request.getParameterMap();
        
        Set keys = params.keySet();
        
        Iterator it = keys.iterator();
        
        while(it.hasNext())
        {
            String key = (String)it.next();
            
            String val = (String)request.getParameter(key);
            
            proxy.writeParam(key, val);
        }
        
        rimResponse = proxy.writeResponseTo();
        
        proxy.close();
        
        return rimResponse;
    }
}
